﻿using BLL;
using Config;
using DBUtility;
using gfLibrary;
using IYAKASAPortal.Services;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IYAKASAPortal.Login
{
    public partial class LoginedList : System.Web.UI.UserControl
    {
        private UserInfo userInfo { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] != null && Session["UserName"].ToString() != "")
            {
                string userName = Session["UserName"].ToString();
                userInfo = IYAKASAPortal.Services.UserAuthority.GetUserAuthority(userName);
            }
            else
            {
                gfJs.RunFunction(this.Page, "parent.window.location = '../logout.aspx';");
            }
            if (!IsPostBack)
            {
                BindList("","");
            }
        }

        public void BindList(string strJCLSH,string strKey)
        {
            string strSql = "select '' as XM, JCLSH, ID,HPHM,HPZL,HPZLDH,JCXH,GWBH,ZYWLB,JYLB,JCZT_STATUS,JYXM FROM VEHICLE_DISPATCH WHERE JCZT_STATUS='0' and DLY='"+userInfo.UserName+"' and JCLSH like '" + strJCLSH + "%'";
            if (strKey.Length > 0)
            {
                strSql += "and ";
                strSql += "(HPHM like '%" + strKey + "%'";
                strSql += " or VIN like '%" + strKey + "%')";
            }

            strSql += " order by ID desc";

            DataTable m_dtTable = new DataTable();
            GreatPager1.BindDataTablePage(ref m_dtTable, AppConfig.g_strConnectionStringSqlClient, strSql, 5);

            for (int i = 0; i < m_dtTable.Rows.Count; i++)
            {
                m_dtTable.Rows[i]["JYXM"] = getJCXM(m_dtTable.Rows[i]["JYXM"].ToString());
                m_dtTable.Rows[i]["XM"] = GetTypeByJCLSH(m_dtTable.Rows[i]["JCLSH"].ToString());
            }

            rptList.DataSource = m_dtTable;
            rptList.DataBind();
            m_dtTable.Dispose();
        }

        protected void drpJYLB_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnSearch_Click(sender, e);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string strJYLB = drpJYLB.SelectedValue;
            string strKey = txtKeywords.Text.Trim();
            BindList(strJYLB, strKey);
        }

        protected void btnSearchALL_Click(object sender, EventArgs e)
        {
            string strJYLB = "";
            string strKey = "";
            txtKeywords.Text = "";
            BindList(strJYLB, strKey);
        }

        protected void GreatPager1_PageIndexChanging(object sender, gfPage.PagerEventArgs e)
        {
            GreatPager1.PageIndex = e.NewPageIndex;

            DataTable m_dtTable = GreatPager1.BindDataTablePage();

            for (int i = 0; i < m_dtTable.Rows.Count; i++)
            {
                m_dtTable.Rows[i]["JYXM"] = getJCXM(m_dtTable.Rows[i]["JYXM"].ToString());
                m_dtTable.Rows[i]["XM"] = GetTypeByJCLSH(m_dtTable.Rows[i]["JCLSH"].ToString());
            }

            rptList.DataSource = m_dtTable;
            rptList.DataBind();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string strUniqueKey = Request.Form["txtUniqueKey"];
            if ((strUniqueKey == null) || (strUniqueKey.Length == 0) || (gfValidate.Is_Integer(strUniqueKey) == false))
            {
                gfJs.SetAlertMsg(this.Page, "请选择一条记录！");
                return;
            }
            VEHICLE_DISPATCH_BLL dispatch = new VEHICLE_DISPATCH_BLL();
            int dispatchId = int.Parse(strUniqueKey);
            string strJCLSH = "";
            strJCLSH = DbHelper.GetValue("select jclsh from VEHICLE_DISPATCH where id='" + dispatchId + "' and JCZT_STATUS='0' ");
            if (!string.IsNullOrEmpty(strJCLSH))
            {
                dispatch.Delete(strJCLSH);
                btnSearch_Click(sender, e);
            }
            else
            {
                gfJs.SetAlertMsg(this.Page, "删除记录失败！");
            }
        }

        protected string getJCXM(string jcxm)
        {
            string result = "";
            if (!string.IsNullOrEmpty(jcxm))
            {
                string temp = jcxm.Substring(0, jcxm.Length - 1);
                if (temp.IndexOf(',') > 0)
                {
                    var item = temp.Split(',');
                    foreach (var i in item)
                    {
                        result += getXMName(i.ToString()) + "、";
                    }
                }
                else
                {
                    result = getXMName(temp) + "、";
                }
                return result.Substring(0, result.Length - 1);
            }
            else
            {
                return result;
            }
        }

        protected string getXMName(string xm)
        {
            string result = "";
            result = DbHelper.GetValue("SELECT CON_CON from const_define where CON_CHNAME='jyxm' and CON_CODE='" + xm + "'");
            return result;
        }

        private string GetTypeByJCLSH(string strJCLSH)
        {
            string strRet = "";
            string strType = strJCLSH.Substring(0, 1);
            switch(strType)
            {
                case "A":
                    strRet = "安检";
                    break;
                case "Z":
                    strRet = "综检";
                    break;
                case "P":
                    strRet = "排放";
                    break;
            }

            return strRet;
        }
    }
}